﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FF.Entity;
using FF.Method;

namespace FF
{
    public partial class FormGood : Form
    {
        public const  int ACTION_TYPE_ADD = 1;
        public const int ACTION_TYPE_MODIFY_DELETE = 2;
        const string GOOD_ID_HEADER = "SP-";
        //const int ACTION_TYPE_DELETE = 3;
        private int actionType = 0;
        private User currentuser;
        private Good good;
        public FormGood()
        {
            InitializeComponent();
        }

        //public FormGood(Good good,int actionType)
        //{
        //    switch (actionType)
        //    {
        //        case ACTION_TYPE_ADD:
        //            this.Text = "添加商品";
        //            break;
        //        case ACTION_TYPE_MODIFY_DELETE:
        //            this.Text = "修改/删除商品";
        //            break;
        //    }
        //    this.actionType = actionType;
        //    this.good = good;
        //}
        public FormGood(User user, int actionType)
        {
            InitializeComponent();
           
            this.Text = "添加商品";
            this.currentuser = user;
                  
            this.actionType = actionType;
           
        }
        public FormGood(string goodid, int actionType)
        {
            InitializeComponent();
           
            this.Text = "修改/删除商品";
            this.good = GoodMethod.getGoodById(goodid);
                  
            this.actionType = actionType;

        }

        private void FormGood_Load(object sender, EventArgs e)
        {
            if(this.actionType == ACTION_TYPE_ADD)
            {
                //this.textBoxId.Text = GoodMethod.generateID(GOOD_ID_HEADER);
                //this.textBoxId.Enabled = false;
                buttonDelete.Visible = false;
                buttonOK.Text = "添加";
            }
            else
            {
                this.textBoxId.Text = good.goodId;
                //this.textBoxId.Enabled = false;
                this.textBoxName.Text = good.name;
                textBoxPrice1.Text = good.price1;
                textBoxPrice2.Text = good.price2;
                textBoxPrice3.Text = good.price3;
                richTextBoxRemark.Text = good.remark;
                buttonOK.Text = "修改";
                buttonDelete.Visible = true ;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            good = new Good();
            good.goodId = textBoxId.Text;
            good.name = textBoxName.Text.Trim();
            good.price1 = textBoxPrice1.Text.Trim();
            good.price2 = textBoxPrice2.Text.Trim();
            good.price3 = textBoxPrice3.Text.Trim();
            good.remark = richTextBoxRemark.Text.Trim();
            if (good.name == "" || good.price1 == "" || good.price2 == "" || good.price3 == "")
            {
                MessageBox.Show("请填写完整信息！");
                return;
            }
           
            good.addTime = DateTime.Now.ToString("s");//Sqlite保存的日期要转为iso 8601标准字符串格式使用 日期.ToString("s");这种方法转换成 iso 8601标准字符串格式了
            if(actionType == ACTION_TYPE_ADD)
            {
                good.addUserId = this.currentuser.UserId.ToString();
                good.addUserName = this.currentuser.UserName;
                if(GoodMethod.addGood(good) == 1)
                {
                    MessageBox.Show("添加成功");
                    textBoxName.Text = "";
                    textBoxPrice2.Text = "";
                    textBoxPrice1.Text = "";
                    textBoxPrice3.Text = "";
                    richTextBoxRemark.Text = "";
                    textBoxName.Focus();
                    //this.Close();
                }else{
                    MessageBox.Show("添加失败");
                }
            }else //修改商品信息
            {
                if (GoodMethod.updateGood(good) == 1)
                {
                    MessageBox.Show("修改成功");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("修改失败");
                    this.Close();
                }
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定删除吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (GoodMethod.deleteGood(textBoxId.Text) == 1)
                {
                    MessageBox.Show("删除成功");

                }
                else
                {
                    MessageBox.Show("删除失败");
                }
                this.Close();
            }
        }

        private void textBoxPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                MessageBox.Show("请输入数字");
                e.Handled = true;
            }
        }

        private void textBoxPrice_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
